/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示对 FLASH 进行擦除、写入、读出验证
  *          样例成功执行后LED(PA03)会进行闪烁，否则会保持常亮
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接J28
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_flash.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
uint8_t AddrBuf[FLASH_PAGE_SIZE];

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/
#define DEMO_PAGE_IDX    63                                           // PAGE 63

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    uint32_t UserStartAddr = FLASH_PAGE_SIZE * DEMO_PAGE_IDX;         // 待操作区域起始地址所在Page的地址
    uint32_t UserEndAddr   = FLASH_PAGE_SIZE * DEMO_PAGE_IDX;         // 待操作区域结束地址所在page的地址
    HAL_StatusTypeDef Flag;
    uint16_t i;

    //------------------------------------------------------------
    HAL_SYSCTRL_FLASH_CLK_ENABLE();

    //------------------------------------------------------------
    HAL_InitTick(4000000);

    Bsp_Led_Init();

    //------------------------------------------------------------
    // 解锁并擦除页面
    HAL_FLASH_UnlockPages(UserStartAddr, UserEndAddr);                // 解锁Page63
    Flag = HAL_FLASH_ErasePages(UserStartAddr, UserEndAddr);          // 擦除Page63
    HAL_FLASH_LockPages();

    if (Flag != HAL_OK)
    {
        while (1);                                                    // 擦除失败
    }

    for (i=0; i<FLASH_PAGE_SIZE; i++)
    {
        if (0xFF != *((volatile uint8_t *)(UserStartAddr + i)))
        {
            while (1);                                                // 对擦除后的结果进行验证失败
        }
    }

    //------------------------------------------------------------
    // 解锁并编程
    for (i=0; i<FLASH_PAGE_SIZE; i++)
    {
        AddrBuf[i] = (uint8_t)i;
    }
    HAL_FLASH_UnlockPages(UserStartAddr, UserEndAddr);                // 解锁Page63
    Flag = HAL_FLASH_WriteBytes(UserStartAddr, AddrBuf, FLASH_PAGE_SIZE);
    HAL_FLASH_LockPages();
    if (Flag != HAL_OK)
    {
        while (1);                                                    // 写入失败
    }

    //------------------------------------------------------------
    // 读取写入Flash的数据进行验证
    for (i=0; i<FLASH_PAGE_SIZE; i++)
    {
        if ((uint8_t)i != *((volatile uint8_t *)(UserStartAddr + i)))
        {
            while (1);                                                // 对写入的数据进行验证失败
        }
    }

    //------------------------------------------------------------
    // 结束
    while (1)
    {
        BSP_LED_TOG();
        HAL_Delay(100);
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
